package com.roadjava.demo00;

import com.roadjava.Application;
import com.roadjava.Builder;
import com.roadjava.beans.dto.CarDTO;
import com.roadjava.beans.dto.DriverDTO;
import com.roadjava.beans.dto.PartDTO;
import com.roadjava.beans.vo.CarVO;
import com.roadjava.beans.vo.DriverVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MapStructTest00 {

    @Test
    public void test() {
        CarDTO carDTO = Builder.buildCarDTO();

        // dto -> vo
        CarVO carVO = new CarVO();
        carVO.setId(carDTO.getId());
        carVO.setVin(carDTO.getVin());
        carVO.setPrice(carDTO.getPrice());

        // double -> 保留两位小数字符串
        double totalPrice = carDTO.getTotalPrice();
        DecimalFormat df = new DecimalFormat("#.00");
        String totalPriceStr = df.format(totalPrice);
        carVO.setTotalPrice(totalPriceStr);

        // Date -> String
        Date publishDate = carDTO.getPublishDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String publishDateFormat = sdf.format(publishDate);
        carVO.setPublishDate(publishDateFormat);

        // Brand -> BrandName
        carVO.setBrandName(carDTO.getBrand());

        List<PartDTO> partDTOS = carDTO.getPartDTOS();
        boolean hasPart = !CollectionUtils.isEmpty(partDTOS);
        carVO.setHasPart(hasPart);

        DriverVO driverVO = new DriverVO();
        DriverDTO driverDTO = carDTO.getDriverDTO();
        driverVO.setDriverId(driverDTO.getId());
        driverVO.setFullName(driverDTO.getName());
        carVO.setDriverVO(driverVO);

        System.out.println(carVO);
    }

}
